//
//  PersonMessageTableViewController.m
//  XiangMuB
//
//  Created by lanou3g on 16/8/13.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "PersonMessageTableViewController.h"

#import "PersonMessageTableViewCell.h"
#import "CommentViewController.h"
#import "ContentModel.h"
#import "CellContentModel.h"
#import "LocalDataHandle.h"
#import "PersonMessage.h"

@interface PersonMessageTableViewController ()

@property (strong, nonatomic) NSMutableArray *dataArray;

@end

@implementation PersonMessageTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor orangeColor];
    self.navigationItem.title = @"我的动态";
    
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self setupData];
    
//    [self setupDataArray];
    
    
    NSLog(@"dataArray = %@",self.dataArray);

}

- (void)setupData{
    
    NSArray *array = [[LocalDataHandle shareDataHandle] getDataWithCellContentModleFromLocal];
    
    for (PersonMessage *person  in array) {
        
        CellContentModel *model = [CellContentModel unarchiveWithCellContentData:person.personData];
        [self.dataArray addObject:model];
        
    }
    
    
    
}


- (void)setupDataArray{
    
    
    AVObject *data = [AVObject objectWithClassName:@"ContentModel" objectId:@"57b166fd0a2b5800632cbe73"];
    
    NSMutableArray *array = [NSMutableArray array];
    [data fetchInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (!error) {
                
                NSLog(@"object = %@",object);
                CellContentModel *model = [[CellContentModel alloc] init];
//                model.contentString = object[@"contentString"];
                
                NSLog(@"=======%@",object[@"contentString"]);
                
                [array addObject:model];
                
                self.dataArray = array;
                [self.tableView reloadData];
                
                
                
            } else {
                
                NSLog(@"加载数据失败");
            }

            
        });
        
        
    }];

    
    
}


#pragma mark getter method

- (NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *indentifierCell = @"cell";
    PersonMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifierCell];
    
    if (cell == nil) {
        
        cell = [[PersonMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifierCell];
        
    }
    
    CellContentModel *model = self.dataArray[indexPath.row];
    
    cell.contentLabel.text = model.textViewString;
//    cell.model = self.dataArray[indexPath.row];
    
    
    return cell;
}

#pragma mark uitableview delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 400;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CommentViewController *comment = [[CommentViewController alloc] init];
    
    [self.navigationController pushViewController:comment animated:YES];
    
}

@end
